﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;

public class GameController : MonoBehaviour
{
    public bool paused;
    // Start is called before the first frame update
    void Start()
    {
        paused = false;
        FindRecursive(GameObject.Find("Main Camera").transform, "失败界面").SetActive(false);
    }

    public static GameObject FindRecursive(Transform transform, string name)
    {
        var result = transform.Find(name);
        if (result != null) return result.gameObject;
        foreach (Transform child in transform)
        {
            GameObject t = FindRecursive(child, name);
            if (t != null && t != default) return t;
        }
        return default;
    }
    // Update is called once per frame
    void Update()
    {
        var space = Input.GetKeyUp(KeyCode.Space);
        var R = Input.GetKeyUp(KeyCode.R);
        if (space)
        {
            var pausePanel = FindRecursive(GameObject.Find("Main Camera").transform, "暂界面");
            if (!paused)
            {
                pausePanel.SetActive(true);
                pausePanel.GetComponent<Animator>().CrossFade("暂停界面", 0.1f, -1, 0); ;
                Time.timeScale = 0f;
            }
            else
            {
                //pausePanel.GetComponent<Animator>.st
                //pausePanel.SetActive(false);
                Time.timeScale = 1f;
            }
            paused = !paused;
        }
        // debug use only
        if (R) {
            
            SceneManager.LoadScene(SceneManager.GetActiveScene().name);
        }
    }

    public void EndGame()
    {
        var failPanel = FindRecursive(GameObject.Find("Main Camera").transform, "失败界面");
        // Debug.Log(failPanel);
        failPanel.SetActive(true);
        GameObject.Find("TrainHead").SetActive(false);

        //Time.timeScale = 0f;

        /*
    #if UNITY_EDITOR
        UnityEditor.EditorApplication.isPlaying = false;
    #else
        Application.Quit();
    #endif
        */
    }
}
